package com.rainyun.rcmc.bukkit;

import com.google.gson.*;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.inventory.Inventory;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Map;

/**Bukkit的Gson支持工具类*/
public class BukGson {

	/**设置GsonBuilder,把bukkit常用实体注册成可用json序列化*/
	public static boolean regAllToGsonBuilder(GsonBuilder gb) {
		Class<ConfigurationSerialization> c = ConfigurationSerialization.class;
		Field aliases = null;
		try {
			aliases = c.getDeclaredField("aliases");
			aliases.setAccessible(true);
			Map<String, Class<? extends ConfigurationSerializable>> map = (Map<String, Class<? extends ConfigurationSerializable>>)aliases.get(null);
			SerializableConverter sc = new SerializableConverter();
			for(Class<?> sclazz:map.values()) {
				gb.registerTypeAdapter(sclazz,sc);
			}
			IteratorConverter ic = new IteratorConverter();
			gb.registerTypeAdapter(Inventory.class,ic);
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public static class SerializableConverter implements JsonSerializer<ConfigurationSerializable>,JsonDeserializer<ConfigurationSerializable> {
		public JsonElement serialize(ConfigurationSerializable item, Type typeOfSrc,
		        JsonSerializationContext context) {
		    if (item == null) {
		        return new JsonPrimitive("");
		    } else {
		    	YamlConfiguration yml = new YamlConfiguration();
		    	yml.set("content",item);
		    	String content = yml.saveToString();
		        return new JsonPrimitive(content);
		    }
		}
		public ConfigurationSerializable deserialize(JsonElement json, Type typeOfT,JsonDeserializationContext context) throws JsonParseException {
			if(json.isJsonPrimitive()) {
				String str = json.getAsJsonPrimitive().getAsString();
				YamlConfiguration yml = new YamlConfiguration();
		    	try {
					yml.loadFromString(str);
				} catch (InvalidConfigurationException e) {
					e.printStackTrace();
				}
		    	Object obj = yml.get("content");
		    	if(obj instanceof ConfigurationSerializable) {
		    		return (ConfigurationSerializable) obj;
		    	}
			}
			return null;
		}
	}
	public static class IteratorConverter implements JsonSerializer<Iterator<?>>,JsonDeserializer<Iterator<?>> {
		public JsonElement serialize(Iterator<?> item, Type typeOfSrc,
				JsonSerializationContext context) {
			if (item == null) {
				return new JsonPrimitive("");
			} else {
				YamlConfiguration yml = new YamlConfiguration();
				yml.set("content",item);
				String content = yml.saveToString();
				return new JsonPrimitive(content);
			}
		}
		public Iterator<?> deserialize(JsonElement json, Type typeOfT,JsonDeserializationContext context) throws JsonParseException {
			if(json.isJsonPrimitive()) {
				String str = json.getAsJsonPrimitive().getAsString();
				YamlConfiguration yml = new YamlConfiguration();
				try {
					yml.loadFromString(str);
				} catch (InvalidConfigurationException e) {
					e.printStackTrace();
				}
				Object obj = yml.get("content");
				if(obj instanceof Iterator) {
					return (Iterator<?>) obj;
				}
			}
			return null;
		}
	}
	
}
